<template>
	<view class="container">
		<view class="user-info">
			<view class="form-item">
				<text class="lable">头像</text>
				<u-upload  @afterRead="afterRead" @delete="deletePic" name="1" :maxCount="1"
					:previewFullImage="false" class="uploadAvator">
					<image :src="avator" mode="widthFix" class="avator"></image>
				</u-upload>
			</view>
			<view class="form-item" @click="goto('/minepkg/change_userName/change_userName')">
				<text class="lable">昵称</text>
				<text class="content">这是昵称</text>
			</view>
			<view class="form-item">
				<text class="lable">用户ID</text>
				<text class="content">1124</text>
			</view>
			<view class="form-item">
				<text class="lable">手机号</text>
				<text class="content">{{phone}}</text>
			</view>
			<view class="form-item">
				<text class="lable">会员</text>
				<text class="content">未绑定</text>
			</view>

		</view>
		<button class="btn" @click="exit()">退出登录</button>
	</view>

</template>

<script>
	import store from '@/store/store.js'
	export default {
		onLoad() {
			this.init()
		},
		data() {
			return {
				userName: '',
				phone: '',
				isMember: '',
				userId: '',
				avator: uni.getStorageSync('avator'),
			};
		},
		methods: {
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				console.log(event)
				// let lists = [].concat(event.file)
				// let fileListLen = this[`fileList${event.name}`].length
				// lists.map((item) => {
				// 	this[`fileList${event.name}`].push({
				// 		...item,
				// 		status: 'uploading',
				// 		message: '上传中'
				// 	})
				// })
				const result = await this.uploadFilePromise(event.file.url)
				const temp = JSON.parse(result)
				const avatorSrc = uni.$http.baseURL + '/file/download?fileId=' + temp.body.fileId
				store.dispatch('user/setAVATOR', avatorSrc)
				this.avator = avatorSrc
				this.updateInfo(avatorSrc)
				// for (let i = 0; i < lists.length; i++) {
				// const result = await this.uploadFilePromise(lists[i].url)
				// let item = this[`fileList${event.name}`][fileListLen]
				// this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
				// 	status: 'success',
				// 	message: '',
				// 	url: result
				// }))
				// fileListLen++
				// }
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://note.zjut.club/file/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							'fileType': 1
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
			async updateInfo(url) {
				const info = {}
				info.headUrl = url
				info.phone = uni.getStorageSync('phone')
				info.userId = uni.getStorageSync('userId')
				info.userName = uni.getStorageSync('userName')
				info.wxId = uni.getStorageSync('openId')
				const {
					data: res
				} = uni.$http.post(uni.$http.baseURL + '/user/updateUserInfo', info)
			},
			init() {
				//初始化个人信息
				this.phone = uni.getStorageSync('phone')
				this.avator = uni.getStorageSync('avator')
				this.userId = uni.getStorageSync('userId')
			},
			goto(url) {
				uni.navigateTo({
					url: url
				})
			},
			exit() {
				uni.showModal({
					content: '是否确认退出登录？',
					cancelColor: '#E62129',
					success() {
						uni.switchTab({
							url: '/pages/mine/mine'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.user-info {
		background: #FFFFFF;
		margin: 20rpx 30rpx 0 30rpx;
		border: 1rpx solid #EBEEF5;
		display: flex;
		flex-direction: column;
		border-radius: 20rpx;

		.form-item {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #EBEEF5;
			height: 100rpx;
			font-size: 28rpx;

			.lable {
				margin-left: 20rpx;

			}

			.uploadAvator {}

			.avator {
				// position: absolute;
				// margin-right: 30rpx;
				margin-left: 480rpx;
				width: 104rpx;
				height: 104rpx;
			}

			.content {

				margin-right: 20rpx;
			}
		}

	}

	.btn {
		margin-top: 480rpx;
		width: 650rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #E62129 0%, #BA000A 100%);
		box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
		border-radius: 44rpx;
	}
</style>
